package com.ntanh.mealplanner.base;

import android.content.Intent;
import android.content.res.Configuration;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.ntanh.mealplanner.R;
import com.ntanh.mealplanner.main.MainActivity;
import com.ntanh.mealplanner.map.MapActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * This activity will add Navigation Drawer for our application and all the code related to navigation drawer.
 * We are going to extend all our other activities from this BaseActivity so that every activity will have Navigation Drawer in it.
 * This activity layout contain one frame layout in which we will add our child activity layout.
 */
public class BaseActivity extends ActionBarActivity {

    /**
     * Frame layout: Which is going to be used as parent layout for child activity layout.
     * This layout is protected so that child activity can access this
     */
    protected FrameLayout frameLayout;

    /**
     * Base layout node of this Activity.
     */
    protected DrawerLayout mDrawerLayout;

    /**
     * ListView to add navigation drawer item in it.
     * We have made it protected to access it in child class. We will just use it in child class to make item selected according to activity opened.
     */
    protected ListView mDrawerList;

    /**
     * Drawer listner class for drawer open, close etc.
     */
    protected ActionBarDrawerToggle mDrawerToggle;

    /**
     * This flag is used just to check that launcher activity is called first time
     * so that we can open appropriate Activity on launch and make list item position selected accordingly.
     */
    private static boolean isLaunch = true;

    /**
     * Static variable for selected item position. Which can be used in child activity to know which item is selected from the list.
     */
    protected static int position;

    protected CharSequence mDrawerTitle;
    protected CharSequence mTitle;
    protected List<DrawerItem> drawerItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        // Initializing
        drawerItems = new ArrayList<>();
        mTitle = mDrawerTitle = getTitle();
        frameLayout = (FrameLayout) findViewById(R.id.content_frame);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        initDrawerList();

        mDrawerList.setAdapter(new CustomDrawerAdapter(this, R.layout.custom_drawer_item, drawerItems, initAccount()));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
            @Override
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
            @Override
            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (isLaunch) {
            isLaunch = false;
            if (savedInstanceState == null) {
                if (drawerItems.get(0).isAccountItem() & drawerItems.get(1).getTitle() != null) {
                    openActivity(2);
                } else if (drawerItems.get(0).getTitle() != null) {
                    openActivity(1);
                } else {
                    openActivity(0);
                }
            }
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (drawerItems.get(position).getTitle() == null) {
                openActivity(position);
                //SelectItem(position);
            }
        }
    }

    private void initDrawerList() {
        // Add Drawer Item to drawerItems
        drawerItems.add(new DrawerItem(true)); // adding a spinner to the list
        drawerItems.add(new DrawerItem(getResources().getString(R.string.feature))); // adding a header to the list
        drawerItems.add(new DrawerItem(getResources().getString(R.string.meal_planner), R.drawable.ic_planner));
        drawerItems.add(new DrawerItem(getResources().getString(R.string.map), R.drawable.ic_recipe));
        drawerItems.add(new DrawerItem(getResources().getString(R.string.other_option))); // adding a header to the list
        drawerItems.add(new DrawerItem(getResources().getString(R.string.setting), R.drawable.ic_action_settings));
        drawerItems.add(new DrawerItem(getResources().getString(R.string.help), R.drawable.ic_action_help));
        drawerItems.add(new DrawerItem(getResources().getString(R.string.about), R.drawable.ic_action_about));
        drawerItems.add(new DrawerItem(getResources().getString(R.string.log_out), R.drawable.ic_log_out));
    }

    private AccountItem initAccount() {
        return new AccountItem(R.drawable.user1, "Anh", "ntanh1402@gmail.com");
    }

    /**
     * @param position Launching activity when any list item is clicked.
     */
    protected void openActivity(int position) {

        /**
         * We can set title & itemChecked here but as this BaseActivity is parent for other activity,
         * So whenever any activity is going to launch this BaseActivity is also going to be called and
         * it will reset this value because of initialization in onCreate method.
         * So that we are setting this in child activity.
         */
        //		mDrawerList.setItemChecked(position, true);
        //		setTitle(listArray[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        BaseActivity.position = position; //Setting currently selected position in this field so that it will be available in our child activities.

        switch (position) {
            case 2:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case 3:
                startActivity(new Intent(this, MapActivity.class));
                break;

            default:
                break;
        }
    }
}
